package com.thinkgem.jeesite.modules.shop.jiekou;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import com.thinkgem.jeesite.common.mapper.JsonMapper;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.CacheUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.shop.entity.*;
import com.thinkgem.jeesite.modules.shop.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.thinkgem.jeesite.modules.shop.entity.*;
import com.thinkgem.jeesite.modules.shop.service.*;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.SystemService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.thinkgem.jeesite.modules.shop.entity.Home;
import com.thinkgem.jeesite.modules.shop.service.HomeService;
import com.thinkgem.jeesite.modules.shop.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import com.thinkgem.jeesite.modules.sys.entity.Office;
import com.thinkgem.jeesite.modules.sys.entity.Role;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.security.FormAuthenticationFilter;
import com.thinkgem.jeesite.modules.sys.security.SystemAuthorizingRealm;
import com.thinkgem.jeesite.modules.sys.security.UsernamePasswordToken;
import com.thinkgem.jeesite.modules.sys.service.SystemService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.ListResourceBundle;
import java.util.Map;

/**
 * @Author: Meisi.Chen
 * @Company: 陕西迈腾软件信息技术有限公司
 * @create: 2019/5/16 14:45
 */
@Controller
@RequestMapping("client")
public class ClientApiController extends BaseController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SystemService systemService;
    @Autowired
    private HomeService homeService;

    @Autowired
    public BannerService bannerService;
    @Autowired
    private CateService cateService;
    @Autowired
    private ProductService productService;

    @Autowired
    private ShopcartService shopcartService;

    @Autowired
    private CollectionsService collectionsService;

    @Autowired
    private CommentsService cs;
    @Autowired
    private ReplyService replyService;
    @Autowired
    private OrdersonService ordersonService;

    @Autowired
    private OrderService orderService;


    //首页显示
    @RequestMapping(value = "index")
    public String index(HttpServletRequest request, HttpServletResponse response, Model model) {
        Banner banner = new Banner();
        List<Banner> list = bannerService.findList(banner);
        model.addAttribute("list", list);

        Cate cate = new Cate();
        cate.setType("1");
        List<Cate> catelist = cateService.findList(cate);
        model.addAttribute("categorylist", catelist);
        Cate cate1 = new Cate();
        cate1.setType("0");
        List<Cate> catelistlist1 = cateService.findList(cate1);
        model.addAttribute("categorylist1", catelistlist1);
        List<Product> products = productService.findList(new Product());
        model.addAttribute("products", products);
        return "modules/moblieshop/home/index";
    }

    //购物车页面
    @RequestMapping(value = "cart")
    public String cart(Model model) {
        User user = UserUtils.getUser();
        if (user.getId() != null) {
            Shopcart shopcart = new Shopcart();
            shopcart.setUser(user);
            List<Shopcart> list = shopcartService.findList(shopcart);
            model.addAttribute("list", list);
            return "modules/moblieshop/easyLife/cart";
        } else {
            return "redirect:login";
        }
    }

    //从购物车删除
    @RequestMapping(value = "cartdel")
    public String cart(String id) {
        Shopcart shopcart = shopcartService.get(id);
        shopcartService.delete(shopcart);
        return "redirect:cart";
    }

    //添加到购物车
    @RequestMapping(value = "cartadd")
    @ResponseBody
    public Map<String, Object> cartadd(String id, String num) {
        Map<String, Object> map = Maps.newHashMap();
        try {
            User user = UserUtils.getUser();
            if (user.getId() != null) {
                Shopcart shopcart = new Shopcart();
                shopcart.setUser(user);
                shopcart.setNum(num);
                Product product = new Product();
                product.setId(id);
                shopcart.setProduct(product);
                shopcartService.save(shopcart);
                map.put("status", "0");
                map.put("message", "添加成功");
                return map;
            } else {
                map.put("status", "2");
                map.put("message", "请先登录");
                return map;
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("status", "1");
            map.put("message", "系统");
            return map;
        }
    }

    //美食界面即商品展示页面
    @RequestMapping(value = "easyLife")
    public String order(String index, String categoryid, Model model) {
        if ("0".equals(index)) {
            Cate cate = new Cate();
            cate.setType("1");
            List<Cate> catelist = cateService.findList(cate);
            model.addAttribute("categorylist", catelist);
            List<Product> productlist = productService.findList(new Product());
            model.addAttribute("productlist", productlist);
        } else if ("1".equals(index)) {
            Cate cate = new Cate();
            cate.setType("1");
            List<Cate> catelist = cateService.findList(cate);
            model.addAttribute("categorylist", catelist);
            cate.setId(categoryid);
            Product product = new Product();
            product.setCategory(cate);
            List<Product> list = productService.findList(product);
            model.addAttribute("productlist", list);
            model.addAttribute("cid", categoryid);

        }
        return "modules/moblieshop/easyLife/easyLife";
    }

    //模糊查询商品
    @RequestMapping(value = "easyLifeSearch")
    public String easyLifeSearch(String search, Model model) {
        Cate cate = new Cate();
        cate.setType("1");
        List<Cate> catelist = cateService.findList(cate);
        model.addAttribute("categorylist", catelist);
        Product product = new Product();
        product.setName(search);
        List<Product> productlist = productService.findList(product);
        model.addAttribute("productlist", productlist);
        return "modules/moblieshop/easyLife/easyLife";
    }

    //商品详情
    @RequestMapping(value = "easyLifedetails")
    public String mine(String productid, Model model) {
        Product product = productService.get(productid);
        Comments comments = new Comments();
        comments.setProduct(product);
        List<Comments> list = cs.findList(comments);
        int i = list.size();
        model.addAttribute("size", i);
        model.addAttribute("comments", list);
        model.addAttribute("product", product);
        return "modules/moblieshop/easyLife/easyLifedetails";
    }

    //选择收货地址页面
    @RequestMapping(value = "order-address")
    public String goodslist(Home home, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (UserUtils.getUser().getId() != null) {
            String rolename = UserUtils.getUser().getRoleNames();
            if (rolename.contains("普通用户")) {
                User user = UserUtils.getUser();
                home.setUser(user);
                Page<Home> page = homeService.findPage(new Page<Home>(request, response), home);
                model.addAttribute("page", page);
            } else {
                Page<Home> page = homeService.findPage(new Page<Home>(request, response), home);
                model.addAttribute("page", page);
            }
            model.addAttribute("orderid", request.getParameter("orderid"));
            return "modules/moblieshop/easyLife/order-address";
        } else {
            return "redirect:" + "login";
        }
    }

    @RequestMapping(value = "adressid")
    public String adressid(Home home, HttpServletRequest request, HttpServletResponse response, Model model) {
        String homeid = request.getParameter("homeid");
        String orderid = request.getParameter("orderid");
        Home home1 = homeService.get(homeid);
        model.addAttribute("homes", home1.getId());
        model.addAttribute("username", home1.getUser().getName());
        model.addAttribute("tel", home1.getTel());
        model.addAttribute("address", home1.getAddress());
        Order order = orderService.get(orderid);
        Orderson orderson = new Orderson();
        orderson.setOrderId(orderid);
        List<Orderson> list = ordersonService.findList(orderson);
        order.setList(list);
        model.addAttribute("order", order);
        String tt = order.getTotals();
        String stt = order.getStatus();
        model.addAttribute("tt", tt);
        model.addAttribute("stt", stt);
        return "modules/moblieshop/easyLife/order-create";
    }

    @RequestMapping(value = "order-create")
    public String collectList(HttpServletRequest request, HttpServletResponse response, Model model, Home home) {
        if (UserUtils.getUser().getId() != null) {
            String id = request.getParameter("id");

            Home home1 = null;
            User user = UserUtils.getUser();
            home.setUser(user);
            home.setDefalut("0");
            List<Home> list2 = homeService.findList(home);
            if (list2.size() != 0) {
                Home home2 = list2.get(0);
                model.addAttribute("homes", home2.getId());
                model.addAttribute("username", home2.getUser().getName());
                model.addAttribute("tel", home2.getTel());
                model.addAttribute("address", home2.getAddress());
                model.addAttribute("home", "");
            } else {
                home.setDefalut("1");
                List<Home> list1 = homeService.findList(home);
                if (list1.size() != 0) {
                    model.addAttribute("home", list1);
                    model.addAttribute("agress", "请点击选择地址");
                } else {
                    model.addAttribute("home", list1);
                    model.addAttribute("agress", "请点击添加地址");
                    /* return "modules/moblieshop/personal/addhome";*/
                }
                model.addAttribute("homes", "");
                model.addAttribute("username", "");
                model.addAttribute("tel", "");
                model.addAttribute("address", "");
            }


            Order order = new Order();
            order.setId(id);
            Orderson orderson = new Orderson();
            orderson.setOrderId(id);
            List<Order> orderList = orderService.findList(order);
            List<Orderson> ordersonList = ordersonService.findList(orderson);
            ArrayList<Orderson> ordersonArrayList = new ArrayList<Orderson>();
            for (int i = 0; i < ordersonList.size(); i++) {
                Product product = new Product();
                Orderson orderson1 = ordersonList.get(i);
                String pId = orderson1.getPId();
                product.setId(pId);
                List<Product> list = productService.findList(product);
                Product product1 = list.get(0);

                orderson1.setProduct(product1);
                ordersonArrayList.add(orderson1);
            }

            Order order1 = orderList.get(0);
            String tt = order1.getTotals();
            String stt = order1.getStatus();
            order1.setList(ordersonArrayList);
            model.addAttribute("order", order1);
            model.addAttribute("tt", tt);
            model.addAttribute("stt", stt);
            return "modules/moblieshop/easyLife/order-create";
        } else {
            return "redirect:login";
        }
    }

    //订单全查询
    @RequestMapping(value = "orderList")
    public String orderList(Model model) {
        Order order = new Order();
        List<Order> list = orderService.findList(order);
        model.addAttribute("list", list);
        return "modules/moblieshop/easyLife/orderList";
    }

    //删除父订单
    @RequestMapping(value = "orderdel")
    public String orderdel(String id) {
        Order order = new Order();
        order.setId(id);
        orderService.delete(order);
        return "redirect:orderList";
    }

    @RequestMapping(value = "business")
    public String goodsDetail(HttpServletRequest request, HttpServletResponse response, Model model) {
        return "modules/moblieshop/mistiming/business";
    }


    @RequestMapping(value = "juandetails")
    public String goodsNav(HttpServletRequest request, HttpServletResponse response, Model model) {
        return "modules/moblieshop/mistiming/juandetails";
    }


    //按分类显示的商品
    @RequestMapping(value = "menupage")
    public String address(String cid, HttpServletRequest request, HttpServletResponse response, Model model) {
        Cate cate1 = new Cate();
        cate1.setId(cid);
        Product product = new Product();
        product.setCategory(cate1);
        List<Product> productlist = productService.findList(product);
        model.addAttribute("productlist", productlist);
        return "modules/moblieshop/mistiming/menupage";
    }

    //模糊查询商品
    @RequestMapping(value = "menupagesearch")
    public String menupagesearch(String search, HttpServletRequest request, HttpServletResponse response, Model model) {
        Product product = new Product();
        product.setName(search);
        List<Product> productlist = productService.findList(product);
        model.addAttribute("productlist", productlist);
        return "modules/moblieshop/mistiming/menupage";
    }


    //优惠页面 即 分类和商品展示
    @RequestMapping(value = "mistiming")
    public String comment(String index, String categoryid, HttpServletRequest request, HttpServletResponse response, Model model) {
        if ("0".equals(index)) {
            Cate cate = new Cate();
            cate.setType("1");
            List<Cate> catelist = cateService.findList(cate);
            model.addAttribute("categorylist", catelist);
            List<Product> productlist = productService.findList(new Product());
            model.addAttribute("productlist", productlist);
        } else if ("1".equals(index)) {
            Cate cate = new Cate();
            cate.setType("1");
            List<Cate> catelist = cateService.findList(cate);
            model.addAttribute("categorylist", catelist);
            cate.setId(categoryid);
            Product product = new Product();
            product.setCategory(cate);
            List<Product> list = productService.findList(product);
            model.addAttribute("productlist", list);
            model.addAttribute("cid", categoryid);

        }
        return "modules/moblieshop/mistiming/mistiming";
    }

    //跳转到收货地址页面并显示收货地址
    @RequestMapping(value = "customer")
    public String customer(Home home, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (UserUtils.getUser().getId() != null) {
            String rolename = UserUtils.getUser().getRoleNames();
            if (rolename.contains("普通用户")) {
                User user = UserUtils.getUser();
                home.setUser(user);
                Page<Home> page = homeService.findPage(new Page<Home>(request, response), home);
                model.addAttribute("page", page);
            } else {
                Page<Home> page = homeService.findPage(new Page<Home>(request, response), home);
                model.addAttribute("page", page);
            }
            return "modules/moblieshop/personal/customer";
        } else {
            return "redirect:" + "login";
        }
    }

    //跳转到地址增加页面
    @RequestMapping(value = "Addressfrom")
    public String addressfrom(HttpServletRequest request, HttpServletResponse response, Model model) {
        return "modules/moblieshop/personal/Addressfrom";
    }

    //修改和添加方法
    @RequestMapping(value = "addhome")
    @ResponseBody
    public Map<String, Object> addhome(Home home, HttpServletRequest request, HttpServletResponse response, Model model) {
        Map<String, Object> map = Maps.newHashMap();
        if (UserUtils.getUser().getId() != null) {

            User user = UserUtils.getUser();
            home.setUser(user);
            String defalut = home.getDefalut();
            if (defalut.equals("0")) {
                homeService.updateAlldefalut(home.getUser().getId());
            }
            homeService.save(home);
            map.put("status", "0");
            map.put("message", "成功");
            return map;
        } else {
            map.put("status", "1");
            map.put("message", "请登陆");
            return map;
        }
    }

    //跳转到地址修改页面 删除页面
    @RequestMapping(value = "updatehome")
    public String updatehome(Home home, HttpServletRequest request, HttpServletResponse response, Model model) {
        String homeid = request.getParameter("homeid");
        Home home1 = homeService.get(homeid);
        model.addAttribute("home", home1);
        return "modules/moblieshop/personal/Addressfrom";
    }

    //删除地址
    @RequestMapping(value = "deletehome")
    public String deletehome(Home home, HttpServletRequest request, HttpServletResponse response, Model model) {
        String homeid = request.getParameter("homeid");
        home.setId(homeid);
        homeService.delete(home);
        return "redirect:" + "customer";
    }

    //收藏页面
    @RequestMapping(value = "collections")
    public String collections(Model model) {
        User user = UserUtils.getUser();
        Collections collections = new Collections();
        collections.setUser(user);
        List<Collections> list = collectionsService.findList(collections);
        model.addAttribute("list", list);
        return "modules/moblieshop/personal/collections";
    }

    //从收藏中删除
    @RequestMapping(value = "collectionsdel")
    public String collectionsdel(String id) {
        Collections collections = collectionsService.get(id);
        collectionsService.delete(collections);
        return "redirect:collections";
    }


    //根据商品id取消收藏
    @RequestMapping(value = "collectionsdel2")
    public String collectionsdel2(String id) {
        User user = UserUtils.getUser();
        String userId = user.getId();
        collectionsService.deleteByupId(userId, id);
        return "redirect:easyLife?index=0";
    }

    //添加到收藏
    @RequestMapping(value = "collectionsadd")
    public String collectionsadd(String id) {
        User user = UserUtils.getUser();
        if (user.getId() != null) {
            Product product = new Product();
            product.setId(id);
            Collections collections = new Collections();
            collections.setUser(user);
            collections.setproduct(product);
            collectionsService.save(collections);
            return "redirect:easyLife?index=0";
        } else {
            return "redirect:login";
        }
    }

    //跳转到登陆页面
    @RequestMapping(value = "login")
    public String login() {
        return "modules/moblieshop/personal/login";
    }

    //实际登陆判断
    @RequestMapping(value = "userlogin")
    @ResponseBody
    public Map<String, Object> loginFail(HttpServletRequest request, HttpServletResponse response) {

        Map<String, Object> map = Maps.newHashMap();
        SystemAuthorizingRealm.Principal principal = UserUtils.getPrincipal();
        // 如果已经登录，则跳转到首页
        if (principal != null) {
            map.put("status", "2");
            map.put("message", "已登陆，请勿重复登陆");
            return map;
        }
        String username = WebUtils.getCleanParam(request, FormAuthenticationFilter.DEFAULT_USERNAME_PARAM);
        boolean rememberMe = WebUtils.isTrue(request, FormAuthenticationFilter.DEFAULT_REMEMBER_ME_PARAM);
        boolean mobile = WebUtils.isTrue(request, FormAuthenticationFilter.DEFAULT_MOBILE_PARAM);
        String pass = request.getParameter("password");
        System.out.println(username);
        System.out.println(pass.toCharArray());
        String host = StringUtils.getRemoteAddr(request);
        try {
            UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username,
                    pass.toCharArray(), rememberMe, host, null, mobile);
            SecurityUtils.getSubject().login(usernamePasswordToken);
            System.out.println(usernamePasswordToken);
            System.out.println("wode");
        } catch (Exception e) {
            map.put("status", "1");
            map.put("message", "账号或密码错误，登录失败");
            return map;
        }
        User user = UserUtils.getUser();
        System.out.println("yonghu" + user);
        if (user != null && StringUtils.isNotEmpty(user.getId())) {
            map.put("status", "0");
            map.put("message", "登录成功");
        }
        System.out.println(map);
        return map;
    }

    //注销退出当前用户and切换用户
    @RequestMapping("logout")
    public String logout() {
        UserUtils.getSubject().logout();
        return "modules/moblieshop/personal/login";
    }

    //个人中心
    @RequestMapping(value = "personal")//个人中心（我的）
    public String personal(HttpServletRequest request, HttpServletResponse response, Model model) {
        return "modules/moblieshop/personal/personal";
    }

    //注销用户
    @RequestMapping(value = "replacepersonal")
    public String replacepersonal(HttpServletRequest request, HttpServletResponse response, Model model) {
        UserUtils.getSubject().logout();
        return "modules/moblieshop/personal/personal";
    }

    //跳转到注册页面
    @RequestMapping(value = "register")
    public String register(HttpServletRequest request, HttpServletResponse response, Model model) {
        return "modules/moblieshop/personal/register";
    }

    //实际注册页面
    @RequestMapping(value = "registeruser")
    @ResponseBody
    public Map<String, Object> registeruser(User sysUser, HttpServletRequest request, HttpServletResponse response, Model model) {
        Map<String, Object> map = Maps.newHashMap();
        String pass = request.getParameter("password");
        String username = request.getParameter("username");
        String name = request.getParameter("name");
        sysUser.setLoginName(username);
        //判断注册账号是否存在
        List<User> users = systemService.findUser(sysUser);
        System.out.println(users.toString());
        if (!(users.isEmpty())) {
            map.put("status", "0");
            map.put("message", "账号已存在");
            return map;
        }
        sysUser.setName(name);
        sysUser.setPassword(SystemService.entryptPassword(pass));

        List<Role> roleList = Lists.newArrayList();
        roleList.add(new Role("6"));
        sysUser.setRoleList(roleList);
        sysUser.setCompany(new Office("1"));
        sysUser.setOffice(new Office("10"));
        sysUser.setCreateBy(sysUser);
        sysUser.setUpdateBy(sysUser);
        System.out.println(roleList.toString());
        systemService.saveUser(sysUser);
        // 清除当前用户缓存
        if (sysUser.getLoginName().equals(UserUtils.getUser().getLoginName())) {
            UserUtils.clearCache();
            //UserUtils.getCacheMap().clear();
        }
        map.put("status", "1");
        map.put("message", "注册成功");
        return map;
    }

    //跳转到设置界面
    @RequestMapping(value = "settings")
    public String settings(HttpServletRequest request, HttpServletResponse response, Model model) {
        return "modules/moblieshop/personal/settings";
    }

    //修改密码
    @RequestMapping(value = "updatepassword")
    public String updatepassword(HttpServletRequest request, HttpServletResponse response, Model model) {
        return "modules/moblieshop/personal/userjob";
    }

    @RequestMapping(value = "userjob")
    public String userjob(HttpServletRequest request, HttpServletResponse response, Model model) {
        return "modules/moblieshop/personal/userjob";
    }

    @RequestMapping(value = "userMistiming")
    public String userMistiming(HttpServletRequest request, HttpServletResponse response, Model model) {
        return "modules/moblieshop/personal/userMistiming";
    }

    @RequestMapping(value = "userorders")
    public String userorders(HttpServletRequest request, HttpServletResponse response, Model model) {
        return "modules/moblieshop/personal/userorders";
    }

    @RequestMapping(value = "comments")
    public String comments(HttpServletRequest request, HttpServletResponse response, Model model, Comments comments) {
        List<Comments> list = cs.findList(comments);
        model.addAttribute("page", list);
        return "modules/moblieshop/video/timeline";
    }

    @RequestMapping(value = "addreply")
    public String addreply(HttpServletRequest request, HttpServletResponse response, Model model, Reply reply, String commentsId, String redPerson) {
        Comments comments = new Comments(commentsId);
        reply.setComments(comments);
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        reply.setRTime(timestamp);
        replyService.save(reply);

        return "redirect:" + "comments";
    }

    @RequestMapping(value = "skip")
    public String video(HttpServletRequest request, HttpServletResponse response, Model model) {
        return "modules/moblieshop/video/timeline-publish";

    }

    @RequestMapping(value = "addcomments")
    public String addcomments(HttpServletRequest request, HttpServletResponse response, Model model, Comments comments) {
        String userId = request.getParameter("userId");
        User user = new User(userId);
        comments.setUser(user);
        String picture = comments.getPicture();
        if (picture != null && picture != "") {
            comments.setPicture(picture.substring(0, picture.length() - 1));
        }
        cs.save(comments);
        return "redirect:comments";
    }

    //订单页面跳转到添加地址页面
    @RequestMapping(value = "addaddress")
    public String addhome(Model model, HttpServletRequest request) {
        model.addAttribute("orderid", request.getParameter("orderid"));
        return "modules/moblieshop/personal/addhome";
    }

    @RequestMapping(value = "orderResult")
    public String orderResult(String id, Model model) {
        Order order = new Order();
        order.setId(id);
        List<Order> list = orderService.findList(order);
        Order order1 = list.get(0);
        order1.setStatus("1");
        orderService.save(order1);
        return "modules/moblieshop/easyLife/order-result";
    }


    @RequestMapping(value = "jiesuan")
    @ResponseBody
    public Map<String, Object> selectOrder(HttpServletRequest request, HttpServletResponse response, Model model, String mapList) {
        Map<String, Object> map = Maps.newHashMap();
        //总价格，sn，
        Order order = new Order();
        String sn = SnUtils.makeOrderNum();
        order.setSn(sn);
        User user = UserUtils.getUser();
        order.setUser(user);
        order.setStatus("0");
        String id = orderService.insertToId(order);
        JSONArray jsonArray = JSONArray.fromObject(mapList);//将String转化为JSONArray对象
        int zongjia = 0;

        List<Orderson> listOrderson = new ArrayList<Orderson>();
        for (int i = 0; i < jsonArray.size(); i++) {


            JSONObject jsonObject = jsonArray.getJSONObject(i);//获取循环体中的每一个JSONObject对象
            String goodsId = jsonObject.optString("goodsId");
            String goodsNum = jsonObject.optString("goodsNum");
            int i1 = Integer.parseInt(goodsNum);
            Orderson orderson = new Orderson();
            orderson.setPId(goodsId);
            orderson.setProductNum(goodsNum);

            orderson.setOrderId(id);
            Product product = new Product();
            product.setId(goodsId);
            List<Product> list = productService.findList(product);
            Product products = list.get(0);
            String price = products.getPrice();

            double i2 = Double.parseDouble(price);

            orderson.setProductPrice(price);

            ordersonService.save(orderson);

            zongjia += i1 * i2;

            listOrderson.add(orderson);
        }
        order.setId(id);
        // order.setUser(user);
        order.setTotals(zongjia + "");
        orderService.save(order);

        map.put("data", id);
        map.put("status", "0");
        map.put("message", "成果");
        return map;


    }

    @RequestMapping(value = "uploadSite")
    @ResponseBody
    public Map<String, Object> uploadSite(HttpServletRequest request) {
        Map<String, Object> map = Maps.newHashMap();
        String imageaddress = "";
        try {
            imageaddress = ImageUtils.fileUploadImage(request);
            map.put("url", imageaddress);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("status", "-1");
            map.put("message", "上传图片失败,请稍后重试");
            return map;
        }
        map.put("status", "0");
        map.put("message", "上传图片成功");
        return map;
    }


    @RequestMapping(value = "ljgm")
    public void ljgm(HttpServletRequest request, HttpServletResponse response, Model model) throws ServletException, IOException {

        if (UserUtils.getUser().getId() != null) {


            String goodsid = request.getParameter("goodsid");
            String goodsnum = request.getParameter("goodsnum");

            Order order = new Order();
            order.setUser(UserUtils.getUser());
            String sn = SnUtils.makeOrderNum();
            User user = UserUtils.getUser();
            order.setSn(sn);
            order.setUser(user);

            String id = orderService.insertToId(order);


            int i1 = Integer.parseInt(goodsnum);

            Orderson orderson = new Orderson();

            orderson.setPId(goodsid);
            orderson.setProductNum(goodsnum + "");

            orderson.setOrderId(id);
            Product product = new Product();
            product.setId(goodsid);

            List<Product> list = productService.findList(product);
            Product products = list.get(0);
            String price = products.getPrice();

            orderson.setProductPrice(price);
            double i2 = Double.parseDouble(price);


            ordersonService.save(orderson);

            int zongjia = (int) (i1 * i2);

            order.setTotals(zongjia + "");

            orderService.save(order);


            request.getRequestDispatcher("order-create?id=" + id).forward(request, response);

        } else {
            request.getRequestDispatcher("login").forward(request, response);
        }


    }


}

